//
//  YSingleTableViewCell.m
//  STL_ Animation
//
//  Created by dllo on 16/3/31.
//  Copyright © 2016年 STL. All rights reserved.
//

#import "YSingleTableViewCell.h"
#import "STL.h"
@interface YSingleTableViewCell ()

@property (nonatomic, strong) STLLabel *titleLabel;
@property (nonatomic, strong) STLLabel *creatorLabel;
@property (nonatomic, strong) STLLabel *detailLabel;
@property (nonatomic, strong) STLImageView *creatorImageView;
@property (nonatomic, strong) STLImageView *backGroundImageView;
@property (nonatomic, strong) STLView *backGroundView;
@property (nonatomic, strong) CAGradientLayer *layerName;

@end

@implementation YSingleTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self creatViews];
        
        
        
        
    }
    return self;
}

#pragma mark - creatViews
- (void)creatViews{
    self.backGroundView = [[STLView alloc] init];
    [self.contentView addSubview:self.backGroundView];
    
    self.backGroundImageView = [[STLImageView alloc] initWithFrame:CGRectZero];
    [self.backGroundView addSubview:self.backGroundImageView];
    
    self.layerName = [CAGradientLayer layer];
    
    self.layerName.startPoint = CGPointMake(0, 0);
    self.layerName.endPoint = CGPointMake(0, 1);
    self.layerName.colors = @[(__bridge id)[UIColor clearColor].CGColor,(__bridge id)[UIColor blackColor].CGColor];
    self.layerName.locations = @[@(0.5f),@(1.0f)];
    [self.contentView.layer addSublayer:self.layerName];
    
    self.creatorImageView = [[STLImageView alloc] initWithFrame:CGRectZero];
    
    [self.contentView addSubview:self.creatorImageView];
    
    self.titleLabel = [[STLLabel alloc] initWithFrame:CGRectZero];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    self.titleLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.titleLabel];
    
    self.creatorLabel = [[STLLabel alloc] initWithFrame:CGRectZero];
    self.creatorLabel.textColor = [UIColor colorWithRed:8/255.0 green:146/255.0 blue:114/255.0 alpha:1];
    self.creatorLabel.textAlignment = NSTextAlignmentLeft;
    self.creatorLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.creatorLabel];
    
    self.detailLabel = [[STLLabel alloc] initWithFrame:CGRectZero];
    self.detailLabel.textColor = [UIColor whiteColor];
    self.detailLabel.textAlignment = NSTextAlignmentLeft;
    self.creatorLabel.font = [UIFont systemFontOfSize:13];
    [self.contentView addSubview:self.detailLabel];
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
//    self.backGroundView.frame = self.bounds;
    
    self.backGroundImageView.frame = self.bounds;
    
    self.creatorImageView.frame = CGRectMake(5, BoundsHeight - 69, 64, 64);
    self.creatorImageView.layer.cornerRadius = self.creatorImageView.bounds.size.width / 2;
    self.creatorImageView.layer.masksToBounds = YES;
    self.titleLabel.frame = CGRectMake(self.creatorImageView.bounds.size.height + 10, self.creatorImageView.frame.origin.y, BoundsWidth / 2, (self.creatorImageView.bounds.size.height - 4) / 3);
    
    self.creatorLabel.frame = CGRectMake(self.titleLabel.frame.origin.x, self.titleLabel.frame.origin.y + self.titleLabel.bounds.size.height + 2, self.titleLabel.bounds.size.width, self.titleLabel.bounds.size.height);
    
    self.detailLabel.frame = CGRectMake(self.titleLabel.frame.origin.x, self.creatorLabel.frame.origin.y + self.creatorLabel.bounds.size.height + 2, BoundsWidth - self.creatorImageView.bounds.size.width - 20, self.titleLabel.bounds.size.height);
    self.layerName.frame = self.backGroundImageView.bounds;
}

- (void)setYSingleBaseModel:(YSingleBaseModel *)ySingleBaseModel {
    _ySingleBaseModel = ySingleBaseModel;
    
    [self.backGroundImageView sd_setImageWithURL:[NSURL URLWithString:_ySingleBaseModel.thumbnailPic] placeholderImage:[UIImage imageNamed:@"logoimage"]];
    
    [self.creatorImageView sd_setImageWithURL:[NSURL URLWithString:_ySingleBaseModel.creatorModel.largeAvatar] placeholderImage:[UIImage imageNamed:@"logoimage"]];
    
    self.titleLabel.text = _ySingleBaseModel.title;
    self.creatorLabel.text = _ySingleBaseModel.creatorModel.nickName;
    
    NSString *detailStr = [NSString stringWithFormat:@"收录高清MV%@首 获得积分:%@ 播放:%@", _ySingleBaseModel.videoCount, _ySingleBaseModel.integral, _ySingleBaseModel.totalViews];
    
    NSRange videoCount = [detailStr rangeOfString:[NSString stringWithFormat:@"%@", _ySingleBaseModel.videoCount]];
    NSRange integral = [detailStr rangeOfString:[NSString stringWithFormat:@"%@", _ySingleBaseModel.integral]];
    NSRange totalViews = [detailStr rangeOfString:[NSString stringWithFormat:@"%@", _ySingleBaseModel.totalViews]];
    
    NSMutableAttributedString *detailAttStr = [[NSMutableAttributedString alloc] initWithString:detailStr];
    
    [detailAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:8/255.0 green:146/255.0 blue:114/255.0 alpha:1] range:videoCount];
    [detailAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:8/255.0 green:146/255.0 blue:114/255.0 alpha:1] range:integral];
    [detailAttStr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:8/255.0 green:146/255.0 blue:114/255.0 alpha:1] range:totalViews];
    [detailAttStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13] range:NSMakeRange(0, detailAttStr.length)];
    
    self.detailLabel.attributedText = detailAttStr;
    
}
@end
